<?php
/**
 *
 *
 * @author: mgli<mgli@kaikeba.com>
 * @date: 2021/3/1
 * @version: 1.0
 * @copyright: mgli@kaikeba.com @ 2021
 * @filename: TeamTable.php
 */

namespace App\Admin\RenderAble;

use App\Admin\Repositories\MemberTask;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;
use Illuminate\Support\Facades\DB;

class TaskTable extends LazyRenderable
{
    /**
     * 创建表格.
     *
     * @return Grid
     */
    public function grid(): Grid
    {
        $status = $this->status ?? null;
        $progress = $this->progress ?? null;

        return Grid::make(new MemberTask(['member.info']), function(Grid $grid) use ($status, $progress){

            $grid->model()
                ->leftjoin('task_member_map', 'task_member_map.task_id', '=', 'member_task.task_id')
                ->where('task_member_map.member_id', request('key'));
            if (!is_null($status)){
                $grid->model()->where('status', $status);
                if ($status == \App\Models\MemberTask::TASK_STATUS_COMPLETE){
                    $grid->model()->where(DB::raw("DATE_SUB(CURDATE(), INTERVAL 7 DAY)"), "<=", DB::raw("DATE(member_task.completed_at)"));
                }
            }
            if ($progress){
                $grid->model()->where('progress', $progress);
            }
            $grid->model()->orderBy('member_task.status')->orderBy('member_task.task_id', 'desc');

            $grid->column('task_id', '任务ID');
            $grid->column('member', '执行人')->display(function($users){
                $user_str = [];
                foreach ($users as $user){
                    $user_str[] = '<span class="label" style="background:#d2d6de;color: #555">'.$user['info']['member_name'].'</span>';
                }
                return implode(' ', $user_str);
            });
            $grid->column('project_name', '项目名称');
            $grid->column('task_name', '任务名称');

            $grid->column('priority', '优先级')->display(function ($item){
                return \App\Models\MemberTask::$priority_list[$item];
            })->badge(\App\Models\MemberTask::$priority_color);

            $grid->column('progressBar', '时间进度')
                ->width(100)
                ->display(function (){
                    $start_time = strtotime($this->start_time);
                    $end_time = strtotime($this->end_time);
                    $current_time = time();
                    if (empty($start_time) || empty($end_time)){
                        return 0;
                    }
                    if ($current_time < $start_time) {
                        return 0;
                    }elseif($current_time > $end_time){
                        return 100;
                    }
                    return ceil(($current_time - $start_time)/($end_time - $start_time)*100);
                })->progressBar();

            $grid->column('progress', '进展情况')->display(function($item){
                return \App\Models\MemberTask::$progress_list[$item];
            })->dot(\App\Models\MemberTask::$progress_color);

            $grid->column('status', '任务状态')
                ->select(\App\Models\MemberTask::$status_list);
            if ($status == \App\Models\MemberTask::TASK_STATUS_COMPLETE){
                $grid->column('completed_at', '完成时间');
            }
            $grid->column('start_time', '开始时间');
            $grid->column('end_time', '结束时间');

            $grid->disableRowSelector();
            $grid->disableActions();
            $grid->disableFilter();
            $grid->disableFilterButton();
            $grid->disableRefreshButton();
            $grid->disableCreateButton();
            $grid->disablePagination();
            $grid->disablePerPages();
        });
    }
}